package com.honeybee.cloud.framework.activiti.task;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description 测试
 * @Author DELL
 * @Create 2019-04-16
 * @Since 1.0.0
 */
@Component
public class TestTask implements InitializingBean {

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    public void test() {
        // 根据bpmn文件部署流程
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("审批.bpmn").deploy();
        // 获取流程定义
        ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();
        // 启动流程定义，返回流程实例
        ProcessInstance pi = runtimeService
                .startProcessInstanceById(processDefinition.getId());
        String processId = pi.getId();
        System.out.println("流程创建成功，当前流程实例ID：" + processId);

        Task task = taskService.createTaskQuery().processInstanceId(processId)
                .singleResult();
        System.out.println("第一次执行前，任务名称：" + task.getName());
        taskService.complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId)
                .singleResult();
        System.out.println("task为null，任务执行完毕：" + task);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        test();
    }
}
